package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.Tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1.创建文件夹和文件
 * 2.写入数据
 * 3.读取数据
 * 4.封装数据
 */
public class NavServiceImpl implements INavService {
    @Override
    public NavModel[] getNavModels() {

        return null;
    }

    @Override
    public List<NavModel> getNavModelList() {

//        1.创建文件
        IOTool.createNote(Path.PATH, Path.FILE_NAME_NAV);
//        2.写入文件
        List<String> data = setNavData();
        IOTool.writeData(data);
//        3.读取文件
        String string = IOTool.readData();
//        4.封装数据
//        经理少写变量
        return setData(string);
    }

    private List<NavModel> setData(String string) {
        System.out.println(string);
        String[] split = string.split(IOTool.SPLIT);
//        String href = split[0];
        String href = Tool.replace(split[0]);
//        href = href.substring(1, href.length() - 1);
//        href = href.replace("[", "");
//        href = href.replace("]", "");
//        String title = split[1];
//        title = title.replace("[", "");
//        title = title.replace("]", "");
        String title = Tool.replace(split[1]);
        String[] hrefs = href.split(Path.COMMA);
        String[] titles = title.split(Path.COMMA);
//        NavModel[] navModels = new NavModel[6];//数组的动态定义
        List<NavModel> navModelList = new ArrayList<>();
//        NavDao navDao = new NavDao();
        for (int i = 0; i < titles.length; i++) {
            NavModel navModel = new NavModel();
            navModel.setTitle(titles[i]);
            navModel.setHref(hrefs[i]);
//            navModels[i] = navModel;
            navModelList.add(navModel);
        }
        return navModelList;
    }

    private List<String> setNavData() {
        NavDao navDao = new NavDao();
        List<String> list = new ArrayList<>();
//        StringBuilder stringBuilder = new StringBuilder();
//        int i = 0;
//        for (String aa : navDao.hrefs
//        ) {
//            i++;
//            if (navDao.hrefs.length == i) {
//                stringBuilder.append(aa);
//            } else {
//                stringBuilder.append(aa).append(",");
//            }
//        }
//        for (int j = 0; j < navDao.hrefs.length; j++) {
//            if (navDao.hrefs.length != i) {
//                stringBuilder.append(navDao.hrefs[i]).append(",");
//            } else {
//                stringBuilder.append(navDao.hrefs[i]);
//            }
//        }
//        substring 截取字符串
//        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);
//        System.out.println(substring);

        list.add(Arrays.toString(navDao.hrefs));
        list.add(Arrays.toString(navDao.titles));
//        List list1=new ArrayList();
//        List list2=null;
//        list2.size();
//        int size = list1.size();
//        System.out.println(size);
        return list;
    }

    public static void main(String[] args) {
        new NavServiceImpl().getNavModelList();
    }

}
